package emby.writer;

import java.io.OutputStream;
import java.io.PrintStream;

import emby.ASTNode;
import emby.expr.Expression;
import emby.expr.Variable;
import emby.lang.Block;
import emby.lang.EmbyFunction;

public class BasicCodifier implements IEmbyCodifier{

	PrintStream ps;
	int numtabs;
	public BasicCodifier(OutputStream s){
		ps = new PrintStream(s);
		numtabs = 0;
	}
	
	private void printTabs(){
		for(int i=0; i<numtabs; i++) ps.print("\t");
	}
	
	private void tab(){
		numtabs++;
	}
	
	private void untab(){
		numtabs--;
	}
	
	@Override
	public void block(ASTNode[] nodes) {
		for (int i = 0; i < nodes.length; i++) {
			printTabs();
			nodes[i].codify(this);
			ps.println();
		}
	}

	@Override
	public void literal(Object val) {
		ps.print(val);
	}

	@Override
	public void variable(String name) {
		ps.print(name);
	}

	@Override
	public void list(Expression[] elements) {
		ps.print("[");
		for (int i = 0; i < elements.length; i++) {
			elements[i].codify(this);
			if( i< elements.length-1){
				ps.print(",");
			}
		}
		ps.print("]");
	}

	@Override
	public void fncall(Expression op, Expression[] args) {
		op.codify(this);
		ps.print("(");
		for (int i = 0; i < args.length; i++) {
			args[i].codify(this);
			if( i< args.length-1){
				ps.print(",");
			}
		}
		ps.print(")");
	}

	@Override
	public void opcall(EmbyFunction op, Expression[] args) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void lambda(Variable[] params, Block body) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void index(Expression arg, Expression index) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dot(Expression arg1, String property) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void assignment(Variable var, Expression val) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void compassign(Variable var, EmbyFunction op, Expression val) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void fndef(String name, Block body, Variable[] params) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void while_(Expression condition, Block body) {
		// TODO Auto-generated method stub
		printTabs();
		ps.print("while(");
		condition.codify(this);
		ps.print("){\n");
		tab();
		body.codify(this);
		untab();
		printTabs();
		ps.println("}");
	}

	@Override
	public void for_(ASTNode initializer, Expression condition,
			ASTNode accumulator, Block body) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void fore(Variable var, Expression iterable, Block body) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void if_(Expression condition, Block body) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ifelse(Expression condition, Block ifbody, Block elsebody) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void ret(Expression val) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void throw_(Expression val) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void trycatch(Block trybody, Variable catvar, Block catbody) {
		// TODO Auto-generated method stub
		
	}

}
